<?php
    class Admin_RoleController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_role;
        
        public function init(){
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_role = new Admin_Model_Role();
        }
        
        public function preDispatch(){
        	$auth = Zend_Auth::getInstance();
        	if(!$auth->hasIdentity()){
        		if($this->_request->getActionName() != 'login'){
        			$this->_redirect('admin/index/login');
        		}
        	}
        }
        
        public function indexAction(){
            $this->view->headTitle('Quản lý phân quyền');
            
            if($_POST['add']){
                $this->_redirect('admin/role/create');
            }
            if($_POST['refresh']){
				$this->_redirect('admin/role');
			}
            if($_POST['delete']){
                $listId = $this->_arrParam['check'];
				$this->_role->deleteRoles($listId);
				$this->_redirect($_SERVER['HTTP_REFERER']);
            }
            
            $list = $this->_role->listRole();
            $adapter = new Zend_Paginator_Adapter_DbSelect($list);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function createAction(){
            $this->view->headTitle('Thêm quyền mới');
            $filter = new Zend_Filter_StringTrim();
            $role_name = $filter->filter($this->_arrParam['role_name']);
            
            $validate = new Admin_Form_ValidateRole();
            if($this->_request->isPost()){
                if($validate->validateRoleName($role_name)){
                    if($this->_role->checkRole($role_name) > 0){
                        $this->view->error = 'Tên quyền đã tồn tại';
                    }
                    else{
                        $flex = new Zendvn_Filter_RemoveCircumflex();
                        $lower  = new Zend_Filter_StringToLower();
                        $this->view->roleNameValue = $role_name;
                        $data = array(
                            'role_name' => $role_name,
                        );
                        $result = $this->_role->addRole($data);
                        
                        $data['role_id'] = $result;
                        $data['role_name_filter'] = $lower->filter($flex->filter($role_name));
                        $this->_role->saveAddItem($data, array('task' => 'add'));
                        $this->_redirect('admin/role');
                    }
                }
                else{
                    $this->view->error = 'Tên quyền không được để trống và phải là chữ cái';
                }
            }
        }
        
        public function editAction(){
            $this->view->headTitle('Sửa quyền hạn');
            $role_id = $this->_arrParam['role_id'];
            $this->view->data = $this->_role->getRoleById($role_id);
            
            if($this->_request->isPost()){
                $filter = new Zend_Filter_StringTrim();
                $role_name = $filter->filter($this->_arrParam['role_name']);
                $validation = new Admin_Form_ValidateRole();
                $flag = 0;
                
                if($validation->validateRoleName($role_name)){
                    $this->view->roleNameValue = $role_name;
                }
                else{
                    $this->view->error = 'Tên quyền không được để trống và phải là chữ cái';
                    $flag++;
                }
                if($this->view->data['role_name'] != $role_name){
                    if($this->_role->checkRole($role_name, $role_id) > 0){
                        $this->view->error = 'Tên quyền đã tồn tại';
                        $flag++;
                    }
                }
                
                if($flag == 0){
                    $flex = new Zendvn_Filter_RemoveCircumflex();
                    $lower = new Zend_Filter_StringToLower();
                    $data = array(
                        'role_name' => $role_name,
                    );
                    $this->_role->editRole($data, $role_id);
                    $data['role_id'] = $role_id;
                    $data['role_name_filter'] = $lower->filter($flex->filter($role_name));
                    $this->_role->saveAddItem($data, array('task' => 'edit'));
                    $this->_redirect('admin/role');
                }
            }
        }
        
        public function deleteAction(){
            $this->_role->deleteRole($this->_arrParam['role_id']);
            $this->_redirect('admin/role');
        }
        
        public function searchAction(){
            $this->view->headTitle('Tìm kiếm quyền hạn');
            $filter = new Zend_Filter_StringTrim();
            $flex   = new Zendvn_Filter_RemoveCircumflex();
            $lower  = new Zend_Filter_StringToLower();
            $flag   = 0;
            
            $keyword = $filter->filter($this->_arrParam['keyword']);
            $this->view->searchValue = $keyword;

            if($_POST['add']){
                $this->_redirect('admin/role/create');
            }
            if($_POST['refresh']){
                $this->_redirect('admin/role');
			}
            if($_POST['delete']){
                $listId = $this->_arrParam['check'];
                $this->_role->deleteRoles($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
			
            $validation = new Admin_Form_ValidateRole();
            if($validation->validateSearch($keyword)){
                    $this->view->searchValue = $keyword;
            }
            else{
                    $this->view->errorSearch = 'Bạn chưa nhập từ khóa';
            $keyword = $lower->filter($flex->filter($keyword));
            $keyword = 'role_name_filter:' . $keyword;
            $result = $this->_role->searchRole($keyword);
            $paginator = Zend_Paginator::factory($result);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);

            $this->view->total = count($result);
            $this->view->data = $paginator;
            $flag++;
            }
			
            if($flag == 0){
                $keyword = $lower->filter($flex->filter($keyword));
                $keyword = 'role_name_filter:' . $keyword;
                $result = $this->_role->searchRole($keyword);
                $paginator = Zend_Paginator::factory($result);
                $paginator->setItemCountPerPage(40);
                $paginator->setPageRange(5);
                $currentPage = $this->_request->getParam('page', 1);
                $paginator->setCurrentPageNumber($currentPage);

                $this->view->total = count($result);
                $this->view->data = $paginator;
                }	
            }
        }
